home *** CD-ROM | disk | FTP | other *** search
- /* signal.c --- BIBLE pp. 95-97 */
- #include <stdio.h>
- #include <signal.h>
- void ctrlc_handler(int);
- int back_again = 0;
- main()
- {
- /* Take over the Control-C interrupt */
- if(signal(SIGINT, ctrlc_handler) == SIG_ERR)
- {
- perror("signal failed");
- exit(0);
- }
- printf("Installed SIGINT signal handler\n");
- printf("Hit Control-C to exit:");
- while(1)
- {
- kbhit();
- if(back_again != 0)
- {
- back_again = 0;
- printf("\nHit Control-C to exit:");
- }
- }
- }
- /* --------------------------------- */
- void ctrlc_handler(int sig)
- {
- int c;
- /* First arrange to ignore further SIGINT */
- signal(SIGINT, SIG_IGN);
- printf("\nInterrupted. Quit?");
- c = getche();
- if(c == 'y' || c == 'Y')
- exit(0);
- /* Reenable interrupt handler -- and return */
- back_again = 1;
- signal(SIGINT, ctrlc_handler);
- }